using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace WebApi.Filters
{
    public class OpenApiDefaultValueSchemaFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (schema == null)
            {
                return;
            }

            foreach (var property in schema.Properties)
            {
                if (property.Value.Default != null)
                {
                    continue;
                }

                // 设置 Swagger 请求体中字符串类型的属性默认值为空字符串
                if (property.Value.Type == "string" && property.Value.Format != "date")
                {
                    property.Value.Default = new OpenApiString(String.Empty);
                }

                if (property.Key == "pageIndex")
                {
                    property.Value.Default = new OpenApiInteger(0);
                }

                if (property.Key == "pageSize")
                {
                    property.Value.Default = new OpenApiInteger(10);
                }
            }
        }
    }
}